﻿import mx.utils.Delegate;
class cn.eminia.AS2.M1 {
	var ctrl:MovieClip;
	static var store:Object = {};
	var scope, onClick, onUp, state, obj;
	static var firstClickTime:Number;
	static var lastClickTime:Number;
	static var id:Number = 0;
	static var n:Number = 0;
	function M1(scope, vars) {
		this.scope = scope;
		this.onClick = vars.click;
		this.onUp = vars.up;
		state = 0;
	}
	static function isClick(a:MovieClip, scope:MovieClip, func):Void {
		var obj = {};
		obj.scope = scope;
		obj.callback = func;
		obj.onMouseDown = Delegate.create(obj, function () {
			if (a.hitTest(_root._xmouse, _root._ymouse)) {
				trace('click');
				Delegate.create(this.scope, this.callback)({it:a, id:cn.eminia.AS2.BoGV3.getLastID(a._name)});
			}
		});
		Mouse.addListener(obj);
	}
	static function onDbClick(scope:MovieClip, mc, func:Function):Object {
		if (mc == 'global') {
			var obj = {};
			obj.scope = scope;
			obj.callback = func;
			obj.mc = 'global';
			obj.onMouseDown = Delegate.create(obj, function () {
				ifDbClick(this);
			});
			Mouse.addListener(obj);
		} else {
			var obj = {};
			obj.scope = scope;
			obj.callback = func;
			obj.mc = mc;
			obj.onMouseDown = Delegate.create(obj, function () {
				if (mc.hitTest(_root._xmouse, _root._ymouse)) {
					ifDbClick(this);
				}
			});
			Mouse.addListener(obj);
		}
		//
		obj.dispose = function() {
			trace('dispose');
			Mouse.removeListener(this);
			this = null;
			delete this;
		};
		//cn.eminia.AS2.M1[id] = obj;
		//id++;
		return obj;
	}
	static function ifDbClick(obj):Void {
		if (obj.n == 0) {
			obj.n = 1;
			obj.firstClickTime = getTimer();
		} else {
			obj.n = 0;
			obj.lastClickTime = getTimer();
		}
		//两次点击时间差在300毫秒以内计为双击 !  
		if (Math.abs(obj.lastClickTime-obj.firstClickTime)<300) {
			//trace("doubleclick");
			//发出事件
			if (obj.mc != 'global') {
				Delegate.create(obj.scope, obj.callback)({it:obj.mc, id:cn.eminia.AS2.BoGV3.getLastID(obj.mc._name)});
			} else {
				Delegate.create(obj.scope, obj.callback)('global');
			}
			//this.dispatchEvent({type:"onDbClick", target:this});
		}
	} 
}
